from PySide.QtGui import *

from core.base import *
from gui.shinylist import *

class ItemView(BaseView):
	def on_data_update(self, items):
		print 'ItemView received update'
		slist = self.slist
		slist.clear()

		# "item" refers to an item in the Mensei System data file.
		# "list_item" refers to a row of a ShinyList
		for item in items:
			list_item = ShinyListItem()

			supplier = item.get_supplier()
			# Some insurance, in case the database ever goes into an inconsistent state
			if supplier:
				supplier_name = supplier.get_name()
			else:
				supplier_name = '<no supplier>'
			
			item_name = item.get_name()
			code = item.get_code()

			list_item.set_main_text('%s %s' % (item_name, code))
			list_item.set_sub_text('%d remaining; supplied by %s' % (item.get_num_stocks(), supplier_name))
			slist.add_item(list_item)

	def init_ui(self):
		# Widgets for the Inventory tab
		inventory_layout = QVBoxLayout() # The layout for the contents of the inventory tab.
		timeframe_layout = QHBoxLayout() # The layoout for the timeframe text and combo box
		inventory_instruction = QLabel() # instructional text for transactions
		add_item_button = QPushButton('Add an Item to Inventory',self) # The button usd to to call the handling of adding new items to the inventory
		inventory_timeframe = QComboBox() # The combo box used to handle the timeframe for the list.
		inventory_caption = QLabel() # Caption for the inventory timeframe
		self.slist = slist = ShinyList()

		# Now let's format these widgets for use.
		inventory_instruction.setText('Click on an individual item in the list in order to see all details about it and edit those details.')

		# And to fix the combo box and its caption
		inventory_caption.setText('Show Items Last Modified From')

		inventory_timeframe.addItem('2 weeks',None)
		inventory_timeframe.addItem('1 month',None)
		inventory_timeframe.addItem('2 months',None)
		inventory_timeframe.addItem('6 months',None)
		inventory_timeframe.addItem('1 year',None)
		inventory_timeframe.addItem('Forever',None)

		# Time to put the timeframe widgets into their own horizontal layout
		timeframe_layout.addWidget(inventory_caption)
		timeframe_layout.addWidget(inventory_timeframe)

		# Now let's put them all together
		inventory_layout.addWidget(inventory_instruction)
		inventory_layout.addLayout(timeframe_layout)
		inventory_layout.addWidget(slist)
		inventory_layout.addWidget(add_item_button)
		
		self.setLayout(inventory_layout) # Set the view's layout

	def __init__(self, parent=None, controller=None):
		super(ItemView, self).__init__(parent, controller)
		self.init_ui()



def main():
	import sys
	app = QApplication(sys.argv)
	
	wnd = ItemView()
	wnd.setWindowTitle('InventoryView')
	wnd.show()
	
	sys.exit(app.exec_())

if __name__ == '__main__':
	main()

